package com.ptszyxx.popose.module.main.user;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.databinding.FragmentUserDetailDynamicBinding;
import com.ptszyxx.popose.module.main.user.adapter.UserDynamicAdapter;
import com.ptszyxx.popose.module.main.user.vm.UserDetailDynamicVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.dynamic.DynamicResult;

import java.util.List;

public class UserDetailDynamicFragment extends BaseFragment<FragmentUserDetailDynamicBinding, UserDetailDynamicVM> {
    private UserDynamicAdapter adapter;
    private String userId;



    public UserDetailDynamicFragment(){
    }
    public UserDetailDynamicFragment(String userId){
        this.userId = userId;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_user_detail_dynamic;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public UserDetailDynamicVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(UserDetailDynamicVM.class);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.userId = userId;
        viewModel.requestList(true);
    }

    @Override
    public void initAdapter() {
        super.initAdapter();
        adapter = new UserDynamicAdapter();
        binding.refreshView.initAdapter(adapter);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onDynamicEvent.observe(this, list -> setAdapterDynamic((List<DynamicResult>)list));
    }

    private void setAdapterDynamic(List<DynamicResult> list) {
        binding.refreshView.setAdapterUI(adapter, list, viewModel.isRefresh());
    }


}
